﻿Imports System.Runtime.CompilerServices

Namespace Xhtml
    Public Module InputExtensions

        <Extension()> _
        Public Function CheckBox(ByVal xHelper As XhtmlHelper, ByVal name As String) As XElement
            Return CheckBox(xHelper, name, Nothing)
        End Function

        <Extension()> _
        Public Function CheckBox(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal checkedValue As String) As XElement
            Return CheckBox(xHelper, name, checkedValue, False)
        End Function

        <Extension()> _
        Public Function CheckBox(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal checkedValue As String, ByVal isChecked As Boolean) As XElement
            Return CheckBox(xHelper, name, checkedValue, isChecked, Nothing)
        End Function

        <Extension()> _
        Public Function CheckBox(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal checkedValue As String, ByVal isChecked As Boolean, ByVal htmlAttributes As Object) As XElement
            Return CheckBox(xHelper, name, checkedValue, isChecked, New RouteValueDictionary(htmlAttributes))
        End Function

        <Extension()> _
        Public Function CheckBox(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal checkedValue As String, ByVal isChecked As Boolean, ByVal htmlAttributes As IDictionary(Of String, Object)) As XElement
            Dim result = <input type="checkbox" name=<%= name %>/>
            If Not String.IsNullOrEmpty(checkedValue) Then
                result.SetAttributeValue("value", checkedValue)
            End If
            If isChecked Then
                result.SetAttributeValue("checked", "checked")
            End If
            If htmlAttributes IsNot Nothing Then
                For Each attr In htmlAttributes
                    result.SetAttributeValue(attr.Key, attr.Value)
                Next
            End If
            Return result
        End Function

        <Extension()> _
        Public Function Hidden(ByVal xHelper As XhtmlHelper, ByVal name As String) As XElement
            ' value 
            Return Hidden(xHelper, name, Nothing)
        End Function

        <Extension()> _
        Public Function Hidden(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object) As XElement
            ' htmlAttributes 
            Return Hidden(xHelper, name, value, Nothing)
        End Function

        <Extension()> _
        Public Function Hidden(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object, ByVal htmlAttributes As Object) As XElement
            Return Hidden(xHelper, name, value, New RouteValueDictionary(htmlAttributes))
        End Function

        <Extension()> _
        Public Function Hidden(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object, ByVal htmlAttributes As IDictionary(Of String, Object)) As XElement
            Dim result = <input type="hidden" name=<%= name %>/>
            If htmlAttributes IsNot Nothing Then
                For Each attr In htmlAttributes
                    result.SetAttributeValue(attr.Key, attr.Value)
                Next
            End If
            If value IsNot Nothing Then
                result.SetAttributeValue("value", value)
            End If
            Return result
        End Function

        <Extension()> _
        Public Function TextBox(ByVal xHelper As XhtmlHelper, ByVal name As String) As XElement
            ' value 
            Return TextBox(xHelper, name, Nothing)
        End Function

        <Extension()> _
        Public Function TextBox(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object) As XElement
            ' htmlAttributes 
            Return TextBox(xHelper, name, value, Nothing)
        End Function

        <Extension()> _
        Public Function TextBox(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object, ByVal htmlAttributes As Object) As XElement
            Return TextBox(xHelper, name, value, New RouteValueDictionary(htmlAttributes))
        End Function

        <Extension()> _
        Public Function TextBox(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object, ByVal htmlAttributes As IDictionary(Of String, Object)) As XElement
            Dim result = <input type="text" name=<%= name %>/>
            If htmlAttributes IsNot Nothing Then
                For Each attr In htmlAttributes
                    result.SetAttributeValue(attr.Key, attr.Value)
                Next
            End If
            If value IsNot Nothing Then
                result.SetAttributeValue("value", value)
            End If
            Dim modelValue As ModelState = Nothing
            If xHelper.ViewData.ModelState.TryGetValue(name, modelValue) Then
                If value Is Nothing Then
                    result.SetAttributeValue("value", modelValue.Value.AttemptedValue)
                End If
                If modelValue.Errors.Count > 0 Then
                    result.SetAttributeValue("class", "input-validation-error")
                End If
            End If
            Return result
        End Function

        <Extension()> _
        Public Function RadioButton(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object) As XElement
            Return RadioButton(xHelper, name, value, Nothing, Nothing)
        End Function

        <Extension()> _
        Public Function RadioButton(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object, ByVal selectedValue As Object) As XElement
            Return RadioButton(xHelper, name, value, selectedValue, Nothing)
        End Function

        <Extension()> _
        Public Function RadioButton(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object, ByVal selectedValue As Object, ByVal htmlAttributes As Object) As XElement
            Return RadioButton(xHelper, name, value, selectedValue, New RouteValueDictionary(htmlAttributes))
        End Function

        <Extension()> _
        Public Function RadioButton(ByVal xHelper As XhtmlHelper, ByVal name As String, ByVal value As Object, ByVal selectedValue As Object, ByVal htmlAttributes As IDictionary(Of String, Object)) As XElement
            Dim result = <input type="radio" name=<%= name %>/>
            If htmlAttributes IsNot Nothing Then
                For Each attr In htmlAttributes
                    result.SetAttributeValue(attr.Key, attr.Value)
                Next
            End If
            If value IsNot Nothing Then
                result.SetAttributeValue("value", value)
            End If
            If value.Equals(selectedValue) Then
                result.@checked = "checked"
            End If
            Return result
        End Function

    End Module
End Namespace